<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="110px">
      <el-form-item label="标题" prop="title">
        <el-input v-model="dataForm.title" placeholder="请输入标题"></el-input>
      </el-form-item>
    <el-form-item label="数据库账号" prop="username">
      <el-input v-model="dataForm.username" placeholder="请输入数据库账号"></el-input>
    </el-form-item>
    <el-form-item label="数据库密码" prop="password">
      <el-input v-model="dataForm.password" show-password placeholder="请输入密码"></el-input>
    </el-form-item>
    <el-form-item label="数据库ip" prop="dataBaseIp">
      <el-input v-model="dataForm.dataBaseIp" placeholder="请输入数据库ip"></el-input>
    </el-form-item>
    <el-form-item label="数据库端口" prop="port">
      <el-input v-model="dataForm.port" placeholder="请输入数据库端口"></el-input>
    </el-form-item>
    <el-form-item label="数据库驱动" prop="dataBaseDrive">
      <el-input v-model="dataForm.dataBaseDrive" placeholder="请输入数据库驱动"></el-input>
    </el-form-item>
    <el-form-item label="数据库类型" prop="dataBaseType">
      <el-radio v-model="dataForm.dataBaseType" :label= 1>mysql</el-radio>
    </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          username: '',
          password: '',
          dataBaseIp: '',
          port: '',
          dataBaseDrive: '',
          dataBaseType: 0,
          title: '',
          remark: '',
          isEnable: '',
          createTime: ''
        },
        dataRule: {
          username: [
            { required: true, message: '数据库账号不能为空', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '数据库密码不能为空', trigger: 'blur' }
          ],
          dataBaseIp: [
            { required: true, message: '数据库ip不能为空', trigger: 'blur' }
          ],
          port: [
            { required: true, message: '数据库端口不能为空', trigger: 'blur' }
          ],
          dataBaseDrive: [
            { required: true, message: '数据库驱动不能为空', trigger: 'blur' }
          ],
          dataBaseType: [
            { required: true, message: '数据库类型(1-mysql)不能为空', trigger: 'blur' }
          ],
          title: [
            { required: true, message: '标题不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/config/dataBaseConfig/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.username = data.data.username
                this.dataForm.password = data.data.password
                this.dataForm.dataBaseIp = data.data.dataBaseIp
                this.dataForm.port = data.data.port
                this.dataForm.dataBaseDrive = data.data.dataBaseDrive
                this.dataForm.dataBaseType = data.data.dataBaseType
                this.dataForm.title = data.data.title
                this.dataForm.remark = data.data.remark
                this.dataForm.isEnable = data.data.isEnable
                this.dataForm.createTime = data.data.createTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/config/dataBaseConfig/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'username': this.dataForm.username,
                'password': this.dataForm.password,
                'dataBaseIp': this.dataForm.dataBaseIp,
                'port': this.dataForm.port,
                'dataBaseDrive': this.dataForm.dataBaseDrive,
                'dataBaseType': this.dataForm.dataBaseType,
                'title': this.dataForm.title,
                'remark': this.dataForm.remark,
                'isEnable': this.dataForm.isEnable,
                'createTime': this.dataForm.createTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
